
import MainTableOperator from '../components/MainRowOperator';
import moment from 'moment';

import ProcessTemplateAction from '../../../public/actions/employing/processTemplate';

export default class ProcessTmp {
    constructor(parent, context) {
        this.comp = context;
    }

    extend = [ProcessTemplateAction]

    // 合并模版
    mergeTemplate = (res) => {
        Object.assign(res.template, {
            dept: res.org_situation.dept,
            org: res.org_situation.org,
            entryinfo: res.batch_add.entryinfo,
            psninfo: res.batch_add.psninfo,
            gridrelation: {
                ...res.template.gridrelation,
                ...res.org_situation.gridrelation,
                ...res.batch_add.gridrelation
            }
        });

        return res;
    }

    // 给主页面表格每行添加操作按钮
    addOperatorButton = (res) => {
        const {props, action} = this.comp;
        const {emp} = props;

        res.template['list'].items.push({
            itemtype: 'customer',
            label: emp.language['gx6008-000046'],/* 国际化处理： 操作*/
            visible: true,
            fixed: 'right',
            width: '150px',
            attrcode: 'operation',
            render: (text, record, index) => {
                return (
                    <MainTableOperator
                        language={emp.language}
                        onClick={action.tableAct.rowClickHandle}
                        record={record}
                        index={index}
                    />
                )
            }
        });

        return res;
    }

    // 给查询弹窗的申请日期添加默认值
    addDefaultSearchData = (res) => {
        res.template['querybill'].items.map((item) => {
            if(item.attrcode === 'apply_date' && !item.initialvalue) {
                let firstValue = moment().date(1).format('YYYY-MM-DD');
                let secondValue = moment().format('YYYY-MM-DD');
                
                item.initialvalue = {
                    display: `${firstValue},${secondValue}`,
                    value: `${firstValue},${secondValue}`
                };
            }
        });

        return res;
    }

    // 给批量新增的查询模版的某几个字段添加条件
    addQueryConditionToMultiAddSearch = (template) => {

        template['multiadd'].items.map((item) => {
            if(item.attrcode === 'hi_psnjob.pk_org' || item.attrcode === 'hi_psnjob.pk_org.code') {
                if(!item.queryCondition) {
                    item.queryCondition = {};
                }
                Object.assign(item.queryCondition, {
                    TreeRefActionExt: 'nccloud.web.hrhi.pub.sqlbuilder.OrgRefSqlBuilder'
                });
            }
        })

        return template;
    }

    // 处理模版数据
    selfProcessTemplate = (res) => {
        if(!res.org_situation) {
            res.org_situation = {};
        }
        if(!res.batch_add) {
            res.batch_add = {};
        }

        res = this.mergeTemplate(res);
        
        res.template = this.addEmptyObjectToFormTemplate(res.template);
        res.template = this.addQueryConditionToMultiAddSearch(res.template);

        res = this.addOperatorButton(res);
        res = this.addDefaultSearchData(res);
        
        
        return res.template
    }
}